package cn.itsource.exam.service.impl;

import cn.itsource.exam.domain.ClassInfo;
import cn.itsource.exam.domain.LogInfo;
import cn.itsource.exam.domain.SubjectInfo;
import cn.itsource.exam.dtos.outdto.SubjectClassOutDTO;
import cn.itsource.exam.enums.LogOperateTypeEnum;
import cn.itsource.exam.mapper.ClassInfoMapper;
import cn.itsource.exam.mapper.SubjectInfoMapper;
import cn.itsource.exam.query.ClassInfoQuery;
import cn.itsource.exam.service.ClassInfoService;
import cn.itsource.exam.service.LogInfoService;
import cn.itsource.exam.util.PageList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by su on 2020/7/8.
 */
@Service
public class ClassInfoServiceImpl implements ClassInfoService {

    @Autowired
    private ClassInfoMapper classInfoMapper;

    @Autowired
    private LogInfoService logInfoService;

    @Autowired
    private SubjectInfoMapper subjectInfoMapper;

    /**
     * 增加或修改班级
     * @param paramIn
     * @return
     */
    @Override
    public String modifyClass(ClassInfo paramIn,String operateuser) {
        try{
            int count = 0;
            if(paramIn.getId() != null){   // 如果ID不为空说明是修改
                paramIn.setModifytime(new Date());
                count = classInfoMapper.updateById(paramIn);
                if(count > 0){
                    logInfoService.addLogInfo(new LogInfo(LogOperateTypeEnum.CLASSES.getId(),"修改班级"+paramIn.getClassname()+"信息",operateuser));
                }
            }else{
                paramIn.setStatus(0);
                paramIn.setAddtime(new Date());
                count = classInfoMapper.insert(paramIn);
                if(count > 0){
                    logInfoService.addLogInfo(new LogInfo(LogOperateTypeEnum.CLASSES.getId(),"新增班级"+paramIn.getClassname()+"信息",operateuser));
                }
            }
            return count > 0 ? "":"操作失败";
        }catch (Exception e){
            e.printStackTrace();
            return "程序错误";
        }
    }

    /**
     * 删除班级
     * @param id
     * @return
     */
    @Override
    public String delClass(Long id,String operateuser) {
        ClassInfo paramIn = classInfoMapper.selectById(id);
        paramIn.setStatus(1);
        paramIn.setModifytime(new Date());
        int count = classInfoMapper.updateById(paramIn);
        if(count > 0){
            logInfoService.addLogInfo(new LogInfo(LogOperateTypeEnum.CLASSES.getId(),"删除班级"+paramIn.getClassname()+"信息",operateuser));
        }
        return count > 0 ? "":"操作失败";
    }

    /**
     * 获取班级详细信息
     * @param id
     * @return
     */
    @Override
    public ClassInfo getClassById(Long id) {
        return classInfoMapper.selectById(id);
    }

    /**
     * 分页查询班级信息
     * @param paramIn
     * @return
     */
    @Override
    public PageList<ClassInfo> getClassListPage(ClassInfoQuery paramIn) {
        Integer count = classInfoMapper.countClassList(paramIn);
        if (count == null || "0".equals(count)){
            return  new PageList<ClassInfo>();
        }
        return new PageList<ClassInfo>(count,classInfoMapper.selectClassList(paramIn));
    }

    @Override
    public List<ClassInfo> getClassList(Integer type) {
        return classInfoMapper.selectList(type);
    }

    /**
     * 学科班级树
     * @return
     */
    @Override
    public List<SubjectClassOutDTO> getSubjectClassTree() {
        // 获取所有的学科
        Map<String,Object> map = new HashMap<>();
        map.put("status",0);
        List<SubjectInfo> subList = subjectInfoMapper.selectByMap(map);

        // 获取所有的班级
        List<ClassInfo> classList = classInfoMapper.selectByMap(map);
        List<SubjectClassOutDTO> outDTOs = new ArrayList<>();
        for(SubjectInfo subjectInfo:subList){
            SubjectClassOutDTO outDTO = new SubjectClassOutDTO();
            outDTO.setLabel(subjectInfo.getSubjectname());
            List<ClassInfo> classs = classList.stream().filter(x -> x.getSubjectid().equals(subjectInfo.getId())).collect(Collectors.toList());
            List<SubjectClassOutDTO> childrens = new ArrayList<>();
            if(classs != null && classs.size() > 0){
                for(ClassInfo c:classs){
                    SubjectClassOutDTO dto = new SubjectClassOutDTO();
                    dto.setId(c.getId());
                    dto.setLabel(c.getClassname());
                    childrens.add(dto);
                }
            }
            outDTO.setChildren(childrens);
            outDTOs.add(outDTO);
        }
        return outDTOs;
    }

    @Override
    public String batchDelUser(String ids,String operateuser) {
        if(StringUtils.isEmpty(ids)){
            return "请选择要删除的数据!";
        }
        List<ClassInfo> list = classInfoMapper.selectBatchIds(Arrays.asList(ids.split(",")));
        int count = classInfoMapper.batchdel(Arrays.asList(ids.split(",")));
        if(count > 0){
            new Thread(){   // 异步插入日志
                public void run(){
                    for(ClassInfo paramIn:list){
                        logInfoService.addLogInfo(new LogInfo(LogOperateTypeEnum.CLASSES.getId(),"删除班级"+paramIn.getClassname()+"信息",operateuser));
                    }
                }
            }.start();
        }
        return count > 0 ? "":"操作失败";
    }
}
